package com.evil.kingdom.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.evil.kingdom.constant.RedisConstant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 监听器
 *
 * @author LY
 * @date 2024/5/189:49
 */
@Slf4j
@Component
@AllArgsConstructor
public class MyApplicationListener {

    private final RedisTemplate<String, Object> redisTemplate;

    /**
     * 监听服务启动
     *
     * @param event event
     */
    @EventListener
    public void listenerApplicationStarted(ApplicationStartedEvent event) {
        log.debug("start listener|应用启动完成");
        Set<String> keys = redisTemplate.keys(RedisConstant.SOCKET_TOKEN_KEY_PREFIX + "*");
        if (CollectionUtil.isNotEmpty(keys)) {
            Long delete = redisTemplate.delete(keys);
            log.debug("start listener|delete socket redis|delete:{}", delete);
        }
    }


    /**
     * 监听多个事件
     */
    @EventListener({ApplicationReadyEvent.class, ApplicationStartedEvent.class})
    public void listenerApplication() {
        log.debug("监听到了多个事件");
    }
}
